

//#include  <msp430x54xA.h>
#include  "Drivers.h"
#include  "..\ESS_CommonDefine.h"

unsigned char ButtonPressCounter;
////////////////////////////////////////////////////////////////////////////
//  Button configuration
//  Switch P2.3, press = high, releae = low
////////////////////////////////////////////////////////////////////////////
void InitButton(){
  // Set up pins used by IO unit
  BUTTON_PORT_SEL &= ~SW1_BUTTON;   // set as I/O port
  BUTTON_PORT_DIR &= ~SW1_BUTTON;   // set as input port
  
  Set_SW_Trigger(SW_Rising_Edge_Trigger);
  
  ButtonPressCounter =0;
  G_Device_Action_Controls &= ~BUTTON_PRESS;
  G_Device_Action_Controls &= ~BUTTON_CLICK;
  G_Device_Action_Controls &= ~BUTTON_LONG_PRESS;
}

void Set_SW_Trigger(unsigned char button_Trigger_Setting){
  
  BUTTON_PORT_IFG &= ~SW1_BUTTON;    // set button IFG cleared 
  
  switch(button_Trigger_Setting){
    case Disable_SW_Trigger:
      BUTTON_PORT_IE &= ~SW1_BUTTON;      // set button interrupt disabled
      G_Device_Action_Controls &= ~SW_TRIGGER;
      break;
    case SW_Rising_Edge_Trigger:
      BUTTON_PORT_IES &= ~SW1_BUTTON;    // set button trigger Lo to Hi edge
      BUTTON_PORT_IE |= SW1_BUTTON;      // set button interrupt enabled
      G_Device_Action_Controls |= SW_TRIGGER;
      break;
    case SW_Falling_Edge_Trigger:
      BUTTON_PORT_IES |= SW1_BUTTON;     // set button trigger Hi to Lo edge
      BUTTON_PORT_IE |= SW1_BUTTON;      // set button interrupt enabled
      G_Device_Action_Controls |= SW_TRIGGER;
      break;
    default:
      BUTTON_PORT_IE &= ~SW1_BUTTON;      // set button interrupt disabled
      G_Device_Action_Controls &= ~SW_TRIGGER;
  }
}

unsigned char getSW1Status(){
  return ((BUTTON_PORT_IN & SW1_BUTTON)== 0)? false : true;
}


// Port  interrupt service routine
#pragma vector=BUTTON_PORT_VECTOR
__interrupt void Port(void)
{
  if(G_System_Control_Bit_Status & LOW_POWER_MODE_0_Flag){
    __bic_SR_register_on_exit(LPM0_bits); // Exit active CPU
    G_System_Control_Bit_Status &= ~LOW_POWER_MODE_0_Flag;
  }
  ButtonPressCounter = 0;
  G_Device_Action_Controls |= BUTTON_PRESS;
  BUTTON_PORT_IFG &= ~SW1_BUTTON;    // set button IFG cleared 
}
